package org.apache.struts.pharmacy.pharmacy.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.pharmacy.pharmacy.model.HL7Message;
import org.apache.struts.pharmacy.pharmacy.model.PrescriptionDrugs;

import com.earogya.communicate.Communicator;
import com.earogya.logic.pharmacy.prescription.CreateRDEO11;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/***
 * Class consists with the struts action to prescription drugs to a patient.
 * @author klanerolle@gmail.com
 *
 */
public class PrescriptionDrugsAction extends ActionSupport{
	private PrescriptionDrugs prescriptionDrugs;
	private HL7Message message;
	private Date date;
	
	public HL7Message getMessage() {
		return message;
	}
	
	public PrescriptionDrugs getPrescriptionDrugs() {
		return prescriptionDrugs;
	}

	public void setPrescriptionDrugs(PrescriptionDrugs prescriptionDrugs) {
		this.prescriptionDrugs = prescriptionDrugs;
	}

	/**
	 * Overrides the execute method is ActionSupport interface,When the action occurs this method is executed.
	 * returns ERROR if there is an error while sending, receiving or processing the received message.
	 * returns SUCCESS if there are no errors.
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		CreateRDEO11 createMessage=new CreateRDEO11(prescriptionDrugs);
		String msg=createMessage.createMessage((String)session.get("facility"));
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
		}
		else{
			message.setMessage("empty");
		}
		return SUCCESS;
	}
	
	/**
	 * Overrides the validate method in ActionSupport interface.
	 * Validates the input fields for correct input.
	 */
	public void validate(){
		if(prescriptionDrugs.getPatientId().length()== 0){
			addFieldError( "prescriptionDrugs.patientId", "Patient ID required" );
		}else if(!this.isInteger(prescriptionDrugs.getPatientId())){
			addFieldError( "prescriptionDrugs.patientId", "Incorrect patient ID format." );
		}
		
		if(prescriptionDrugs.getRequestId().length()== 0){
			addFieldError( "prescriptionDrugs.requestId", "Request ID required" );
		}else if(!this.isInteger(prescriptionDrugs.getRequestId())){
			addFieldError( "prescriptionDrugs.requestId", "Incorrect Request ID format." );
		}
		
		if(prescriptionDrugs.getDrugId().length()== 0){
			addFieldError( "prescriptionDrugs.drugId", "Drug ID required" );
		}
		
		if(prescriptionDrugs.getDosage().length()== 0){
			addFieldError( "prescriptionDrugs.dosage", "Dosage required" );
		}

		if(prescriptionDrugs.getMinAmount().length()== 0){
			addFieldError( "prescriptionDrugs.minAmount", "Minimum Amount of Dosage required" );
		}else if(!this.isInteger(prescriptionDrugs.getMinAmount())){
			addFieldError( "prescriptionDrugs.minAmount", "Minimum Amount should be integer" );
		}
		
		if(prescriptionDrugs.getMaxAmount().length()== 0){
			addFieldError( "prescriptionDrugs.maxAmount", "Maximum Amount of Dosage required" );
		}else if(!this.isInteger(prescriptionDrugs.getMaxAmount())){
			addFieldError( "prescriptionDrugs.maxAmount", "Maximum Amount should be integer" );
		}else if(Integer.parseInt(prescriptionDrugs.getMaxAmount())<Integer.parseInt(prescriptionDrugs.getMinAmount())){
			addFieldError( "prescriptionDrugs.maxAmount", "Maximum Amount should be higher than Min Amount" );
		}
		
		if(prescriptionDrugs.getAmountUnits().length()== 0){
			addFieldError( "prescriptionDrugs.amountUnits", "Amount/Units required" );
		}
		
		if(prescriptionDrugs.getQuantity().length()== 0){
			addFieldError( "prescriptionDrugs.quantity", "Quantity required" );
		}else if(!this.isInteger(prescriptionDrugs.getQuantity())){
			addFieldError( "prescriptionDrugs.quantity", "Incorrect Quantity format." );
		}
		
		if(prescriptionDrugs.getAllowSubstitutes().length()== 0){
			addFieldError( "prescriptionDrugs.allowSubstitutes", "Allow Substitutes required" );
		}
		
		if(prescriptionDrugs.getPrescriptionNo().length()== 0){
			addFieldError( "prescriptionDrugs.prescriptionNo", "Prescription Number is required" );
		}else if(!this.isInteger(prescriptionDrugs.getPrescriptionNo())){
			addFieldError( "prescriptionDrugs.prescriptionNo", "Incorrect Prescription Number format." );
		}
		
		if(prescriptionDrugs.getPrescribedByID().length()== 0){
			addFieldError( "prescriptionDrugs.prescribedByID", "Prescription By Id required" );
		}else if(!this.isInteger(prescriptionDrugs.getPrescribedByID())){
			addFieldError( "prescriptionDrugs.prescribedByID", "Incorrect Prescription By Id format." );
		}
		
		if(prescriptionDrugs.getRequestDate()== null){
			addFieldError( "prescriptionDrugs.requestDate", "Prescription Date required" );
		}	
	}
	
	/**
	 * Check if the given string is an integer.
	 * @param value
	 * @return true is the value is an integer
	 */
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
}
